<?php
namespace app\manager\controller;

use app\common\lib\AdminController;
use app\manager\model\MenuModel;
use app\manager\model\validate\MenuValidate;
use think\Controller;
use think\Exception;
use think\Request;

class MenuController extends AdminController
{
    public function index()
    {
        if ($this->request->isAjax()) { // 搜索获取list数据的ajax请求
            $child = input('child', '');
            if ($child) { // 子搜索
                return $this->child_list();
            } else { // 父搜索
                return $this->getList();
            }
        }
        return $this->fetch();
    }

    private function getList() {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');

        $model = new MenuModel();
        if ($keyword) {
            $count = $model->where('title|desc|route', 'like', '%' . $keyword . '%')->where(['level' => $model::LEVEL_ONE])->count();
            $list = $model
                ->where('title|desc|route', 'like', '%' . $keyword . '%')
                ->where(['level' => $model::LEVEL_ONE])
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $model->field('id')->where(['level' => $model::LEVEL_ONE])->count(); //查询满足要求的总记录数
            $list = $model
                ->order(array($sortName=>$sortOrder))
                ->where(['level' => $model::LEVEL_ONE])
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    /**
     * 首页的子菜单list
     *
     * @return mixed
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function child_list() {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $p_id = input('p_id','');

        $model = new MenuModel();
        if (empty($p_id)) {
            throw new Exception('p_id not exist');

        }
        $count = $model->where(['p_id' => $p_id])->count();
        $list = $model
            ->where(['p_id' => $p_id])
            ->order([$sortName => $sortOrder])
            ->limit($offset . ',' . $limit)
            ->select();

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = $_REQUEST;
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new MenuValidate();
            if (!$validateM->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }
            $model = new MenuModel($data);
            $res = $model->allowField(true)->save();
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }

            return json(['code' => 1, 'msg' => 'success']);
        }

        $this->assign('parent_list', MenuModel::oneLevelList());
        $this->assign('status_list', MenuModel::statusList());
        $this->assign('level_list', MenuModel::levelList());
        $this->assign('type_list', MenuModel::typeList());
        return $this->fetch();
    }

    public function update()
    {
        $data = $_REQUEST;
        $model = new MenuModel();
        if (request()->isPost()) {
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new MenuValidate();
            if (!$validateM->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }
            $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }

            return json(['code' => 1, 'msg' => 'success']);
        }
        $Rs = $model->where(['id' => $data['id']])->find();
        $this->assign('Rs', $Rs->getData());
        $this->assign('parent_list', MenuModel::oneLevelList());
        $this->assign('status_list', MenuModel::statusList());
        $this->assign('level_list', MenuModel::levelList());
        $this->assign('type_list', MenuModel::typeList());
        return $this->fetch();
    }

    public function lock() {
        $data = $_REQUEST;
        $model = new MenuModel();
        $m = $model->where(['id' => $data['id']])->find()->getData();
        $status = $model::STATUS_OK;
        if ($m['status'] == $model::STATUS_OK) {
            $status = $model::STATUS_LOCK;
        }
        $model->where(['id' => $data['id']])->update(['status' => $status]);
        return json(['code' => 1, 'msg' => 'success']);
    }

    public function delete() {
        $data = $_REQUEST;
        $model = new MenuModel();
        if (!$model->where(['id' => $data['id']])->delete()) {
            return json(['code' => 0, 'msg' => '删除失败']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }
}
